package cn.iocoder.yudao.module.shop.controller.admin.crosspriceitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.crosspriceitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.crosspriceitem.ShopCrossPriceItemDO;
import cn.iocoder.yudao.module.shop.convert.crosspriceitem.ShopCrossPriceItemConvert;
import cn.iocoder.yudao.module.shop.service.crosspriceitem.ShopCrossPriceItemService;

@Tag(name = "管理后台 - 跨店铺项目对应")
@RestController
@RequestMapping("/shop/cross-price-item")
@Validated
public class ShopCrossPriceItemController {

    @Resource
    private ShopCrossPriceItemService crossPriceItemService;

    @PostMapping("/create")
    @Operation(summary = "创建跨店铺项目对应")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:create')")
    public CommonResult<Long> createCrossPriceItem(@Valid @RequestBody ShopCrossPriceItemCreateReqVO createReqVO) {
        return success(crossPriceItemService.createCrossPriceItem(createReqVO));
    }

    @PostMapping("/batch")
    @Operation(summary = "创建或更新跨店项目")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:create')")
    public CommonResult<Boolean> updateOrCreateBatch(@Valid @RequestBody ShopCrossPriceItemBatchCreateReqVO reqVO){
        return success(crossPriceItemService.updateOrCreateBatch(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨店铺项目对应")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:update')")
    public CommonResult<Boolean> updateCrossPriceItem(@Valid @RequestBody ShopCrossPriceItemUpdateReqVO updateReqVO) {
        crossPriceItemService.updateCrossPriceItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨店铺项目对应")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:delete')")
    public CommonResult<Boolean> deleteCrossPriceItem(@RequestParam("id") Long id) {
        crossPriceItemService.deleteCrossPriceItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨店铺项目对应")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:query')")
    public CommonResult<ShopCrossPriceItemRespVO> getCrossPriceItem(@RequestParam("id") Long id) {
        ShopCrossPriceItemDO crossPriceItem = crossPriceItemService.getCrossPriceItem(id);
        return success(ShopCrossPriceItemConvert.INSTANCE.convert(crossPriceItem));
    }

    @GetMapping("/get-by-tenantid")
    @Operation(summary = "获得跨店铺项目对应")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:query')")
    public CommonResult<List<ShopCrossPriceItemDO>> getByTenantId(@RequestParam("id") Long tenantId){
        return success(crossPriceItemService.getByTenantId(tenantId));
    }

    @GetMapping("/list")
    @Operation(summary = "获得跨店铺项目对应列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:query')")
    public CommonResult<List<ShopCrossPriceItemRespVO>> getCrossPriceItemList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopCrossPriceItemDO> list = crossPriceItemService.getCrossPriceItemList(ids);
        return success(ShopCrossPriceItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨店铺项目对应分页")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:query')")
    public CommonResult<PageResult<ShopCrossPriceItemRespVO>> getCrossPriceItemPage(@Valid ShopCrossPriceItemPageReqVO pageVO) {
        PageResult<ShopCrossPriceItemDO> pageResult = crossPriceItemService.getCrossPriceItemPage(pageVO);
        return success(ShopCrossPriceItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨店铺项目对应 Excel")
    @PreAuthorize("@ss.hasPermission('shop:cross-price-item:export')")
    @OperateLog(type = EXPORT)
    public void exportCrossPriceItemExcel(@Valid ShopCrossPriceItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopCrossPriceItemDO> list = crossPriceItemService.getCrossPriceItemList(exportReqVO);
        // 导出 Excel
        List<ShopCrossPriceItemExcelVO> datas = ShopCrossPriceItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "跨店铺项目对应.xls", "数据", ShopCrossPriceItemExcelVO.class, datas);
    }

}
